package com.manor.paradise.dialog

import android.app.Activity
import android.text.*
import android.text.method.PasswordTransformationMethod
import android.view.View
import cn.qqtheme.framework.entity.Province
import cn.qqtheme.framework.picker.AddressPicker
import com.bumptech.glide.Glide
import com.lxj.xpopup.impl.FullScreenPopupView
import com.manor.common.CommonAppConfig
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.CityUtil
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.DialogUitl
import com.manor.common.utils.ToastUtil
import com.manor.common.interfaces.CommonCallback
import com.manor.common.utils.*
import com.manor.paradise.R
import com.manor.paradise.activity.ShippingAddressActivity
import com.manor.paradise.bean.AddressBean
import com.manor.paradise.bean.GiftsBean
import com.manor.paradise.bean.ParadiseInfoBean
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.dialog_gift.view.*
import java.math.BigDecimal
import java.util.*

/**
 * 兑换礼物
 */
class GiftPopup(val context: Activity, var data: GiftsBean.RecordsBean) : FullScreenPopupView(context), View.OnClickListener {
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_gift
    }

    override fun onCreate() {
        super.onCreate()
        im_dismiss.setOnClickListener(this)
        tv_addaddress.setOnClickListener(this)
        ll_view.setOnClickListener(this)
        tv_context.setOnClickListener(this)
        tv_giftname.setText(data.name)
        tv_mondy.setText(data.sdCoupons.toString())
        ed_password.setInputType(InputType.TYPE_CLASS_NUMBER) //输入类型
        ed_password.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(6))) //最大输入长度
        ed_password.setTransformationMethod(PasswordTransformationMethod.getInstance()) //设置为密码输入框
        ElvesHttpUtil.getParadiseInfo(object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                var paradiseInfoBean: ParadiseInfoBean = GsonUtil.gsonToBean(info, ParadiseInfoBean::class.java)
                tv_ks.setText("我有淘米劵：" + paradiseInfoBean.sdCoupons.toString())

            }

            override fun onError() {
                super.onError()

            }
        })
        ed_number.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                if (!TextUtils.isEmpty(ed_number.text.toString())) {
                    var num: BigDecimal = BigDecimal(ed_number.text.toString())
                    tv_mondy.setText(num.multiply(data.sdCoupons).toString())
                } else {
                    tv_mondy.setText("")
                }

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

        })
        tv_confirm.setOnClickListener({
            if (null != cityList) {
                cityList?.run {
                    if (!TextUtils.isEmpty(ed_number.text.toString()) && !TextUtils.isEmpty(ed_password.text.toString())&&ed_password.text.toString().length==6) {
                        ElvesHttpUtil.getGiftBuy(
                                data.id,
                                mobile,
                                BigDecimal(ed_number.text.toString()),
                                BigDecimal(data.sdCoupons.toString()),
                                ed_password.text.toString(),
                                data.isBlogistics,
                                id,
                                "",
                                object : JHttpCallback() {
                                    override fun onSuccess(code: Int, msg: String, info: String) {
                                        ToastUtil.show(msg)
                                        if (code != 200) {

                                            return
                                        }

                                        dismiss()

                                    }

                                    override fun onError() {
                                        super.onError()

                                    }
                                })
                    }else{
                        if(TextUtils.isEmpty(ed_number.text.toString())){
                            ToastUtil.show("请输入兑换数量")
                        }
                        if(ed_password.text.toString().length<6){
                            ToastUtil.show("请输入6位交易密码")
                        }
                        if(TextUtils.isEmpty(ed_password.text.toString())){
                            ToastUtil.show("请输入交易密码")
                        }
                    }
                }
            }else{
                ToastUtil.show("请选择地址")
            }
        })













        ElvesHttpUtil.getDefaults( object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
              var arr :   AddressBean.RecordsBean = GsonUtil.gsonToBean(info, AddressBean.RecordsBean::class.java)
                if(null!=arr.provinceName){
                    setCity(arr)
                }

            }

            override fun onError() {
                super.onError()

            }
        })
    }


    override fun onClick(v: View?) {
        if (ClickUtil.canClick(500))
            when (v?.id) {
                R.id.im_dismiss -> {
                    dismiss()
                }
                R.id.tv_addaddress -> {
                    ShippingAddressActivity.forward(context, "1")
                    //   context.startActivity(Intent(context, ShippingAddressActivity::class.java))
                }
                R.id.ll_view -> {
                    ShippingAddressActivity.forward(context, "1")
                }
                R.id.tv_context -> {
                    ShippingAddressActivity.forward(context, "1")
                }

            }
    }

    fun setCity(cityList: AddressBean.RecordsBean) {
        this.cityList = cityList
        tv_addaddress.visibility = View.GONE
        ll_view.visibility = View.VISIBLE
        tv_context.visibility = View.VISIBLE
        tv_name.visibility = View.VISIBLE
        tv_tel.visibility = View.VISIBLE
        tv_name.setText(cityList.contacts)
        tv_tel.setText(cityList.mobile)
        tv_context.setText(cityList.provinceName + cityList.cityName + cityList.areaName + cityList.address.toString())
    }

    /**
     * 选择城市
     */
    private fun chooseCity() {
        val list = CityUtil.getInstance().cityList
        if (list == null || list.size == 0) {
            val loading = DialogUitl.loadingDialog(context)
            loading.show()
            CityUtil.getInstance().getCityListFromAssets(object : CommonCallback<ArrayList<Province>>() {
                override fun callback(newList: ArrayList<Province>?) {
                    loading.dismiss()
                    if (newList != null) {
                        showChooseCityDialog(newList)
                    }
                }
            })
        } else {
            showChooseCityDialog(list)
        }
    }

    private var cityList: AddressBean.RecordsBean? = null
    private var mProvinceVal: String? = null
    private var mCityVal: String? = null
    private var mZoneVal: String? = null
    /**
     * 选择城市
     */
    private fun showChooseCityDialog(list: ArrayList<Province>?) {
        var province = mProvinceVal
        var city = mCityVal
        var district = mZoneVal
        if (TextUtils.isEmpty(province)) {
            province = CommonAppConfig.getInstance().province
        }
        if (TextUtils.isEmpty(city)) {
            city = CommonAppConfig.getInstance().city
        }
        if (TextUtils.isEmpty(district)) {
            district = CommonAppConfig.getInstance().district
        }
        DialogUitl.showCityChooseDialog(context, list, province, city, district, AddressPicker.OnAddressPickListener { province, city, county ->
            val provinceName = province.areaName
            val cityName = city.areaName
            val zoneName = county.areaName
            mProvinceVal = provinceName
            mCityVal = cityName
            mZoneVal = zoneName
            KLog.e("city", provinceName + cityName + zoneName)

        })
    }

    override fun onDestroy() {
        Glide.get(context).clearMemory()
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_GIFT_BUY)

        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_SHOP_BY_UID)
        super.onDestroy()
    }
}